package cn.stylefeng.guns.czlm.service;


import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.czlm.entity.TbUser;
import cn.stylefeng.guns.czlm.mapper.TbUserMapper;
import cn.stylefeng.roses.core.util.HttpContext;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class TbUserService extends ServiceImpl<TbUserMapper, TbUser> {

    @Resource
    private TbUserMapper mapper;

    public Page<Map<String, Object>> selectUsers(TbUser tbUser){
        //通过layui获得分页信息
        Page page = LayuiPageFactory.defaultPage();
        Page<Map<String, Object>> userPage = this.mapper.selectUsers(page,tbUser);

        HttpServletRequest request = HttpContext.getRequest();
        Integer pageIndex=Integer.parseInt(request.getParameter("page"));
        if(pageIndex>userPage.getPages()){
            //以下重写了 LayuiPageFactory.defaultPage();
            //每页多少条数据
            int limit = Integer.valueOf(request.getParameter("limit"));
            page=   new Page(1, limit);
            return mapper.selectUsers(page,tbUser);
        }

        return userPage;

    }

    /**
     *修改用户
     * @param tbUser
     */
    public void updateUserStatus(TbUser tbUser){
        mapper.updateUserStatus(tbUser);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    //按id查询
    public TbUser selectById(Long id){
        return mapper.selectById(id);
    }

    /**
     * 修改用户信息
     */
    public void updateTbUser(TbUser tbUser){
        mapper.updateTbUser(tbUser);
    }

    /**
     * 添加用户信息
     */
    public void insertTbUser(TbUser tbUser){
        mapper.insertTbUser(tbUser);
    }
}
